package com.yunao.service.report.esou;

import com.lowagie.text.*;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.rtf.RtfWriter2;

import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;


public class ItextUtil {

	public static String FAMILY_TITLE = "宋体标题";

	public static String FAMILY_TEXT = "宋体正文";

	public static Color COLOR_RED = new Color(204,0,0);

	public static Color COLOR_BLUE = new Color(107, 155, 220);

	public static Color COLOR_BLACK = new Color(0, 0, 0);

	/**
	 * 样式
	 *
	 * @param family
	 *            字体
	 * @param size
	 *            大小
	 * @param color
	 *            颜色
	 * @param styles
	 *            加粗、斜体、下划线等...
	 * @return Font
	 */
	public static Font createStyle(String family, float size, Color color, int... styles) {
		Font font = new Font();
		font.setFamily(family);
		font.setSize(size);
		font.setColor(color);
		if (styles != null)
			for (int style : styles) {
				font.setStyle(style);
			}
		return font;
	}

	public static Document open(String filePath) throws Exception {
		// 设置纸张大小
		Document doc = new Document(PageSize.A4);
		// 建立一个书写器，与 doc 对象关联
		RtfWriter2.getInstance(doc, new FileOutputStream(filePath));
		doc.open();
		return doc;
	}

	public static com.lowagie.text.Image createImage(String imgPath, int align, float width, float height) throws Exception {
		Image img = Image.getInstance(imgPath);
		img.setAlignment(align);
		img.scaleAbsoluteWidth(width);
		img.scaleAbsoluteHeight(height);
		return img;
	}

	public static Paragraph newline() {
		Paragraph paragraph = new Paragraph("", new Font());
		paragraph.setAlignment(Element.ALIGN_LEFT);
		paragraph.setSpacingAfter(10);
		return paragraph;
	}

	public static Paragraph createParagraph(String text, Font font, int align, float indent) {
		Paragraph paragraph = new Paragraph(text, font);
		paragraph.setAlignment(align);
		paragraph.setFirstLineIndent(indent);
		return paragraph;
	}

	public static Paragraph createParagraph(String text, Font font, int align, int sBefore, int sAfter) {
		Paragraph paragraph = new Paragraph(text, font);
		paragraph.setAlignment(align);
		paragraph.setSpacingBefore(sBefore);
		paragraph.setSpacingAfter(sAfter);
		return paragraph;
	}

	public static Paragraph createParagraph(String text, Font font, int align, int sBefore, int sAfter, float indent) {
		Paragraph paragraph = new Paragraph(text, font);
		paragraph.setAlignment(align);
		paragraph.setSpacingBefore(sBefore);
		paragraph.setSpacingAfter(sAfter);
		// paragraph.setFirstLineIndent(indent);
		paragraph.setIndentationLeft(indent);
		return paragraph;
	}

	public static Anchor createAnchor(String text, String url, Font font) {
		Anchor anchor = new Anchor(text, font);
		anchor.setReference(url);
		return anchor;
	}

	public static String filepath(int id) {
		ClassLoader loader = ItextUtil.class.getClassLoader();
		if (loader == null)
			return null;
		String name = ItextUtil.class.getName().replace('.', '/') + ".class";
		URL url = loader.getResource(name);
		if (url == null)
			return null;
		String path = url.getFile();
		String res = "";
		if (path.indexOf("WEB-INF/") != -1) {
			// web project
			path = path.substring((path.indexOf("file:/") >= 0) ? 5 : 0, path.indexOf("WEB-INF/"));
			File file = new File(path + "WEB-INF/conf/");
			if (file.exists())
				res = new File(path + "WEB-INF/").getAbsolutePath() + File.separator;
			else
				res = new File(path).getParentFile().getAbsolutePath() + File.separator;
		} else {
			// java project
			res = new File(".").getAbsolutePath();
			res = res.substring(0, res.length() - 1);
		}
		String filePath = res + "export" + File.separator;
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		filePath += c.get(Calendar.YEAR) + File.separator + c.get(Calendar.MONTH) + File.separator
				+ c.get(Calendar.DAY_OF_MONTH) + File.separator + id + File.separator;
		File file = new File(filePath);
		if (!file.exists())
			file.mkdirs();
		return filePath;
	}
	public static String docpath() {
		ClassLoader loader = ItextUtil.class.getClassLoader();
		if (loader == null)
			return null;
		String name = ItextUtil.class.getName().replace('.', '/') + ".class";
		URL url = loader.getResource(name);
		if (url == null)
			return null;
		String path = url.getFile();
		String res = "";
		if (path.indexOf("WEB-INF/") != -1) {
			// web project
			path = path.substring((path.indexOf("file:/") >= 0) ? 5 : 0, path.indexOf("WEB-INF/"));
			File file = new File(path + "WEB-INF/conf/");
			if (file.exists())
				res = new File(path + "WEB-INF/").getAbsolutePath() + File.separator;
			else
				res = new File(path).getParentFile().getAbsolutePath() + File.separator;
		} else {
			// java project
			res = new File(".").getAbsolutePath();
			res = res.substring(0, res.length() - 1);
		}
		String filePath = res + "doc" + File.separator;
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		filePath += c.get(Calendar.YEAR) + File.separator + c.get(Calendar.MONTH) + File.separator
				+ c.get(Calendar.DAY_OF_MONTH) + File.separator;
		File file = new File(filePath);
		if (!file.exists())
			file.mkdirs();
		return filePath;
	}
//
//	public static void main(String[] args) {
//		String href = "http://www.baidu.com/s?wd=%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F&rsv_spt=1&issp=1&rsv_bp=0&ie=utf-8&tn=baiduhome_pg&rsv_sug3=6&rsv_sug1=6&rsv_sug4=151&oq=zhengze&rsp=0&f=3&rsv_sug5=0";
//		href = href.substring(href.indexOf("http://") + 7);
//		href = href.substring(0, href.indexOf("/"));
//		System.out.println(href);
//	}

}
